package com.kzd.btgame.jetpack.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.kzd.btgame.base.BaseViewModel
import com.kzd.btgame.entity.*
import com.kzd.btgame.jetpack.api.*
import com.module.lemlin.http.*
import com.module.lemlin.owner.setRxLifeScope

class BargainViewModel(application: Application) : BaseViewModel(application) {

    val bargainDetailsLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataT<BargainDetails>>>()

    fun bargainDetails(bid: String) {
        setRxLifeScope(SubscribeViewModel(bargainDetailsLiveData)) {
            newTradeInfo(bid)
        }
    }


    val gamePlayedLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GamePlayed>>>()
    fun bargainGame() {
        setRxLifeScope(SubscribeViewModel(gamePlayedLiveData)) {
            personalMyGame()
        }
    }


    val gameSmallLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameSmall>>>()
    fun bargainGameSmall(gameId: String, appid: String) {
        setRxLifeScope(SubscribeViewModel(gameSmallLiveData)) {
            personalGameSmallInfo(gameId, appid)
        }
    }

    val bargainSubmitLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun bargainSubmit(
        name: String,
        game_id: String,
        introduction: String,
        sname: String,
        small_id: String,
        two_passwd: String? = null,
        money: String,
        system: String,
        images: String,
    ) {
        setRxLifeScope(SubscribeViewModel(bargainSubmitLiveData)) {
            personalTradeAdd(
                name, game_id, introduction, sname, small_id, two_passwd, money, system, images
            )
        }
    }

    private var page: Int = 1

    val bargainRecordLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<BargainRecord>>>()

    fun bargainRecord(type: Int, isLoadMore: Boolean = false) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataE<BargainRecord>>(bargainRecordLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<BargainRecord>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            appTradeMy(page = page.toString(), type = type.toString())
        }
    }

    val bargainPaymentLiveData = MutableLiveData<HttpResponseBody<BargainPaymentResponse>>()
    fun bargainPayment(type: Int, goodsId: String, money: String) {
        setRxLifeScope(object : SubscribeViewModel<BargainPaymentResponse>(bargainPaymentLiveData) {
            override fun onSuccess(resp: BargainPaymentResponse) {
                super.onSuccess(resp.apply {
                    this.type = type
                    this.id = goodsId
                })
                if (type == 1) {
                    userMine()
                }
            }
        }) {
            appTradeBuy(type = type.toString(), goodsId = goodsId, money = money)
        }
    }
}